Imports System.Windows.Forms
Imports System.drawing
Public Class DataGridViewMaskedTextBox
    Inherits DataGridView





    Public Class DataGridViewMaskedTextBoxColumn
        Inherits DataGridViewColumn


        Private maskValue As String = ""

        Public Sub New()
            MyBase.New(New DataGridViewMaskedTextBoxCell())
        End Sub

        Public Property Mask() As String
            Get
                Return Me.maskValue
            End Get

            Set(ByVal value As String)
                Me.maskValue = value
            End Set

        End Property

        Public Overrides Function Clone() As Object
            Dim col As DataGridViewMaskedTextBoxColumn = _
                        CType(MyBase.Clone(), DataGridViewMaskedTextBoxColumn)
            col.Mask = Me.Mask

            Return col
        End Function

        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get

            Set(ByVal value As DataGridViewCell)
                If Not TypeOf value Is DataGridViewMaskedTextBoxCell Then
                    Throw New InvalidCastException("DataGridViewMaskedTextBoxCell")
                End If

                MyBase.CellTemplate = value
            End Set
        End Property

    End Class

    Public Class DataGridViewMaskedTextBoxCell
        Inherits DataGridViewTextBoxCell

        Public Sub New()

        End Sub

        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, _
        ByVal dataGridViewCellStyle As DataGridViewCellStyle)

            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

            Dim maskedBox As DataGridViewMaskedTextBoxEditingControl = CType(Me.DataGridView.EditingControl, DataGridViewMaskedTextBoxEditingControl)

            If Not (maskedBox Is Nothing) Then

                Dim column As DataGridViewMaskedTextBoxColumn = CType(Me.OwningColumn, DataGridViewMaskedTextBoxColumn)

                If Not (column Is Nothing) Then

                    maskedBox.Mask = column.Mask

                End If

                maskedBox.Text = CStr(IIf(Me.Value Is Nothing, "", Me.Value))

            End If

        End Sub

        Public Overrides ReadOnly Property EditType() As Type

            Get

                Return GetType(DataGridViewMaskedTextBoxEditingControl)

            End Get

        End Property

        Public Overrides ReadOnly Property ValueType() As Type

            Get

                Return GetType(Object)

            End Get

        End Property

        Public Overrides ReadOnly Property DefaultNewRowValue() As Object

            Get

                Return MyBase.DefaultNewRowValue

            End Get

        End Property

    End Class

    Public Class DataGridViewMaskedTextBoxEditingControl
        Inherits MaskedTextBox
        Implements IDataGridViewEditingControl
        Private dataGridView As DataGridView

        Private rowIndex As Integer

        Private valueChanged As Boolean

        Public Sub New()
            Me.TabStop = False
        End Sub

        Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
            Return Me.Text
        End Function

        Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
            Get
                Return Me.GetEditingControlFormattedValue( _
                DataGridViewDataErrorContexts.Formatting)
            End Get
            Set(ByVal value As Object)
                Me.Text = CStr(value)
            End Set
        End Property

        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
            Me.Font = dataGridViewCellStyle.Font
            Me.ForeColor = dataGridViewCellStyle.ForeColor
            Me.BackColor = dataGridViewCellStyle.BackColor

            Select Case dataGridViewCellStyle.Alignment
                Case DataGridViewContentAlignment.BottomCenter, DataGridViewContentAlignment.MiddleCenter, DataGridViewContentAlignment.TopCenter
                    Me.TextAlign = HorizontalAlignment.Center
                Case DataGridViewContentAlignment.BottomRight, DataGridViewContentAlignment.MiddleRight, DataGridViewContentAlignment.TopRight
                    Me.TextAlign = HorizontalAlignment.Right
                Case Else
                    Me.TextAlign = HorizontalAlignment.Left
            End Select

        End Sub

        Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return Me.dataGridView
            End Get

            Set(ByVal value As DataGridView)
                Me.dataGridView = value
            End Set
        End Property

        Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return Me.rowIndex
            End Get
            Set(ByVal value As Integer)
                Me.rowIndex = value
            End Set
        End Property

        Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return Me.valueChanged
            End Get

            Set(ByVal value As Boolean)

                Me.valueChanged = value

            End Set

        End Property



        Public Function EditingControlWantsInputKey(ByVal keyData As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey

            Select Case keyData And Keys.KeyCode
                Case Keys.Right, Keys.End, Keys.Left, Keys.Home

                    Return True

                Case Else

                    Return False

            End Select

        End Function

        Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor

            Get

                Return MyBase.Cursor

            End Get

        End Property



        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

            If selectAll Then

                Me.SelectAll()

            Else

                Me.SelectionStart = Me.TextLength

            End If

        End Sub

        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property

        Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
            MyBase.OnTextChanged(e)
            Me.valueChanged = True
            Me.dataGridView.NotifyCurrentCellDirty(True)
        End Sub

        Protected Overrides Sub Finalize()
            MyBase.Finalize()
        End Sub
    End Class
End Class